#include "ET++.ph"
#ifdef __GNUG__
#pragma implementation
#endif

#include "PictureItem.h"

#include "Class.h"
#include "Picture.h"

//---- PictureItem -------------------------------------------------------------

NewMetaImpl(PictureItem,VObject, (TP(pic), T(base), TB(deletePicture)));

PictureItem::PictureItem(Picture *p, const Point &e, int bs, bool freeold)
		: pic(p), base(bs), deletePicture(freeold), VObject(e)
{
}

PictureItem::PictureItem(int id, Picture *p, const Point &e, int bs,
		bool freeold) : pic(p), base(bs), deletePicture(freeold), VObject(e, id)
{
}

PictureItem::~PictureItem()
{
	if (deletePicture && pic) {
		delete pic;
		pic= 0;
	}
}

void PictureItem::SetPicture(Picture *p, bool redraw)
{
	if (deletePicture && pic)
		delete pic;
	deletePicture= FALSE;
	pic= p;
	SetContentRect(Rectangle(GetOrigin(), GetMinSize().extent), redraw);
}

Metric PictureItem::GetMinSize()
{
	if (pic) {
		if (contentRect.extent != gPoint_1)
			return Metric(contentRect.extent, base ? base : contentRect.extent.y);
		return Metric(pic->Size(), base ? base : pic->Size().y);
	}
	return VObject::GetMinSize();
}

void PictureItem::Draw(Rectangle r)
{
	GrEraseRect(r);
	if (pic)
		GrShowPicture(contentRect, pic);
}

void PictureItem::DoObserve(int, int part, void*, Object *op)
{
	if (op == pic && part != cPartSenderDied)
		ForceRedraw();
}

OStream& PictureItem::PrintOn (OStream &s)
{
	VObject::PrintOn(s);
	return s << pic SP << base SP;
}

IStream& PictureItem::ReadFrom(IStream &s)
{
	if (deletePicture)
		SafeDelete(pic);
	deletePicture= TRUE;
	VObject::ReadFrom(s);
	return s >> pic >> base;
}

